<?php

/**
*
* @package ppkBB3cker
* @version $Id: message_user.php 1.000 2008-10-04 11:10:00 PPK $
* @copyright (c) 2008 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

error_reporting(0);
@ini_set('register_globals', 0);
@ini_set('magic_quotes_runtime', 0);
@ini_set('magic_quotes_sybase', 0);

$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];
$startsqlcount=0;
function_exists('date_default_timezone_set') ? date_default_timezone_set('Europe/Moscow') : '';

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

require($phpbb_root_path.'config.'.$phpEx);

if($dbms!='mysql')
{
	err('Only mysql supported');
}

$c=@mysql_connect($dbhost.($dbport ? ":{$dbport}" : ''), $dbuser, $dbpasswd);
if(!$c)
{
	err('Error connecting database: '.mysql_error());
}

$s=@mysql_select_db($dbname, $c);
if(!$s)
{
	err('Error selecting database: '.mysql_error($c));
}

define('CONFIG_TABLE', $table_prefix . 'config');
define('TRACKER_CONFIG_TABLE', $table_prefix . 'tracker_config');
define('SMILIES_TABLE', $table_prefix . 'smilies');
define('PPKCHAT_USERS_TABLE', $table_prefix . 'ppkchat_users');
define('PPKCHAT_MESSAGES_TABLE', $table_prefix . 'ppkchat_messages');

my_sql_query("SET NAMES 'utf8'");

$config=array();
$sql="SELECT config_name, config_value FROM ".CONFIG_TABLE." WHERE config_name IN ('smilies_path', 'script_path', 'ppkbb_chat_last_cleanup')";
$result=my_sql_query($sql);
while($row=mysql_fetch_row($result))
{
	$$row[0]=$row[1];
}
mysql_free_result($result);
$sql="SELECT config_name, config_value FROM ".TRACKER_CONFIG_TABLE." WHERE config_name LIKE 'ppkbb\_chat%'";
$result=my_sql_query($sql);
while($row=mysql_fetch_row($result))
{
	$$row[0]=$row[1];
}
mysql_free_result($result);
$dt = time();
$gmq_gpc = get_magic_quotes_gpc() ? 1 : 0;

$forum_id=my_int_val(@$_GET['f']);
$varch=@$_GET['varch'];
$chat_message=$gmq_gpc ? @$_POST['message'] : mysql_real_escape_string(@$_POST['message'], $c);
$del_message=my_int_val(@$_POST['del_mess']);
$set_hidden=@$_POST['set_hidden'];
$get_gmode=@$_POST['get_gmode'];
$mode=@$_POST['mode'];
$to_user=@$_POST['to_user'];
$acomm=@$_POST['acomm'];
$to_room=my_int_val(@$_POST['to_room']);
$to_user_id=$gmq_gpc ? @$_POST['to_user_id'] : mysql_real_escape_string(@$_POST['to_user_id'], $c);
$to_user ? '' : $to_user_id=0;
$chat_key=$gmq_gpc ? @$_GET['chatkey'] : mysql_real_escape_string(@$_GET['chatkey'], $c);
$lang='ru';

$sql='SELECT * FROM ' .PPKCHAT_USERS_TABLE. " WHERE room='{$forum_id}'";
$result=my_sql_query($sql);
$users=$chat_user=array();
while($row=mysql_fetch_array($result))
{
	if($row['room']==$forum_id)
	{
		if($row['chatkey']==$chat_key)
		{
			$chat_user=$row;
		}
		$users[$row['user_id']]=$row;
	}
}
mysql_free_result($result);

$message_table=$user_table=$acommand='';
$reason=$last_mess_id2=$skip_lacheck=0;
intval($ppkbb_chat_murefresh) > 9 ? '' : $ppkbb_chat_murefresh=10;

if(@$chat_user)
{
	if($chat_user['lastaccess'] > $dt)
	{
		userkick(1);
	}
	else if($chat_user['lastaccess'] == 1)
	{
		$sql="DELETE FROM ".PPKCHAT_USERS_TABLE." WHERE room='{$forum_id}' AND user_id='{$chat_user['user_id']}' AND lastaccess = 1";
		userkick(5);
	}

	$rights=$chat_user['rights'];
	$lang=$chat_user['user_lang'];
	$user_dateformat=$chat_user['user_dateformat'];
	$user_dst=$chat_user['user_dst'] * 3600;
	$user_timezone=$chat_user['user_timezone'] * 3600;
	$ppkbb_chat_guests_option=my_split_config($ppkbb_chat_guests_option, 3, array('strval', 'intval', 'strval'));
	$ppkbb_chat_bot=my_split_config($ppkbb_chat_bot, 3, array('my_int_val', 'strval', 'my_int_val'));
	$chat_user['user_id'] < 2 && $ppkbb_chat_guests_option[0] ? $lang=$ppkbb_chat_guests_option[0] : '';
	!$lang ? $lang='ru' : '';
	$lang=basename($lang);
	if(!file_exists("{$phpbb_root_path}language/{$lang}/mods/ppkbb3cker_chat.{$phpEx}"))
	{
		err($lang['CHAT_LANG_ERROR']);
	}
	require_once("{$phpbb_root_path}language/{$lang}/mods/ppkbb3cker_chat.{$phpEx}");
	//0-$is_adm 1-$is_mod 2-$is_canaddchatm 3-$is_canviewchatu
	//4-$is_canviewchatm 5-$is_candelchatm 6-$is_canskipchatktime
	//7-$is_canhiddeninchat 8-$is_canviewhiddinchat 9-$is_cansendchatpm
	//10-$is_canskipdisbutton 11-$is_canusesmilies 12-$is_canusebbcodes
	//13-$is_candelchatm2 14-$is_canusegmode 15-$is_canuseacommands
	//16-$is_canviewarchive 17-$is_canusebbimg
	$ppkbb_chat_marchive=my_split_config($ppkbb_chat_marchive, 2, 'my_int_val');
	$ppkbb_chat_logs=my_split_config($ppkbb_chat_logs, 4, 'my_int_val');

	$rights=my_split_config($rights, 18, 'my_int_val');
	$user_color=$user_rights=array();

	$ppkbb_chat_enable_pm ? '' : $rights[9]=0;
	$ppkbb_chat_enable_gmode ? '' : $rights[14]=0;
	$ppkbb_chat_enable_acommands ? '' : $rights[15]=0;
	$ppkbb_chat_marchive[0] && $rights[16] ? '' :  $varch=0;

	if($rights[15] && $chat_user['user_id'] > 1)
	{
		/* /u /m /k /b /a /q */
		if($acommand=substr($acomm, 0, 2))
		{
			include($phpbb_root_path.'chat/include/cacommchat.'.$phpEx);
		}
	}

	if($del_message)
	{
		if($rights[4] && $rights[5])//is_canviewchatm is_candelchatm
		{
			if($ppkbb_chat_logs[3])
			{
				$sql='SELECT room, user_id FROM ' .PPKCHAT_MESSAGES_TABLE. " WHERE id='{$del_message}'".(!$rights[13] ? " AND user_id='{$chat_user['user_id']}'" : '').(!$ppkbb_chat_bot[2] ? " AND user_id!='0'" : '').' LIMIT 1';
				$result=my_sql_query($sql);
				$del_data=mysql_fetch_row($result);
			}
			$sql='DELETE FROM ' .PPKCHAT_MESSAGES_TABLE. " WHERE id='{$del_message}'".(!$rights[13] ? " AND user_id='{$chat_user['user_id']}' " : '').(!$ppkbb_chat_bot[2] ? " AND user_id!='0'" : '').' LIMIT 1';
			$result=my_sql_query($sql);
			if(mysql_affected_rows($c) && $ppkbb_chat_logs[3])
			{
				@$del_data[1] > 1 ? '' : $del_data[1]=1;
				include_once($phpbb_root_path.'chat/include/caddlogchat.'.$phpEx);
				chat_add_log(3, $chat_user['user_id'], my_int_val(@$del_data[0]), mysql_real_escape_string($del_data[1], $c), 'LOG_CHAT_DELMESS', '');
			}
		}
	}
	else
	{
		if($set_hidden && $rights[3] && $rights[7])//is_canviewchatu is_canhiddeninchat
		{
			$sql='UPDATE ' .PPKCHAT_USERS_TABLE. " SET user_hidden='".($set_hidden=='true' ? 1 : 0)."' WHERE user_id={$chat_user['user_id']} AND room='{$forum_id}' LIMIT 1";
			$result=my_sql_query($sql);
			$users[$chat_user['user_id']]['user_hidden']=$set_hidden=='true' ? 1 : 0;
		}

		$lastpost=0;
		$chat_user['user_id']==$to_user_id ? $to_user_id=0 : '';
		if($chat_message!=='' && $mode!='read')
		{
			include($phpbb_root_path.'chat/include/caddchatm.'.$phpEx);
		}

		$sql='UPDATE ' .PPKCHAT_USERS_TABLE. ' SET lastaccess='.($lastpost ? 'lastaccess-'.$ppkbb_chat_murefresh : "'{$dt}'").($lastpost ? ", lastpost='$lastpost'" : '')." WHERE chatkey='{$chat_key}' AND room='{$forum_id}'";
		$result=my_sql_query($sql);
		if($rights[3])//is_canviewchatu
		{
			/*if($config['ppkbb_chat_autokill_onrefresh'] && !$skip_lacheck && ($dt - $chat_user['lastaccess']) < $config['ppkbb_chat_murefresh'] - 1)
			{
				userkick(2);
			}
			else
			{*/
				$script_path=$script_path!='/' ? $script_path.'/' : '/';
				foreach($users as $v)
				{
					$user_rights[$v['user_id']]=explode(' ', $v['rights']);

					if($v['user_hidden'] && $user_rights[$v['user_id']][7] && !$rights[8])//is_canhiddeninchat is_canviewhiddinchat
					{

					}
					else
					{
						$v['username']=stripslashes($v['username']);
						$v['username'] = str_replace('---', '&minus;&minus;&minus;', $v['username']);
						$user_banned=$v['lastaccess'] > $dt ? 1 : 0;
						$user_table.= '
						<div id="u'.$v['user_id'].'">'.
							($v['user_id'] > 1 ? '<a target="_blank" href="'.$script_path.'memberlist.'.$phpEx.'?mode=viewprofile&amp;u='.$v['user_id'].'">&bull;</a>' : '<a href="javascript:void(0);"'.
							($v['user_id']==$chat_user['user_id'] ? ' style="color:#FF0000;"' : '').'>&bull;</a>').
							($v['user_hidden'] && $rights[8] ? '&nbsp;<img src="./chat/images/overview.png" alt="" />' : '')
							.(/*!$v['user_hidden'] && */!$user_banned && $rights[9] && $user_rights[$v['user_id']][9] && $v['user_id']!=$chat_user['user_id'] ? '&nbsp;<a href="javascript:void(0);" onClick="javascript:insertuname(\''.$v['username'].'\', '.$v['user_id'].');" title="'.$lang['CHAT_PMHLP'].'"><img src="./chat/images/identity.png" alt="'.$lang['CHAT_PM'].'" /></a>' : '').'&nbsp;<a'.
							($user_banned ? ' title="'.sprintf($lang['CHAT_UBANNED'], date('Y-m-d H:i:s', $v['lastaccess'])).'"' :
							($rights[0] ? ' title="IP: '.long2ip($v['user_ip']).'"' : '')).' style="font-weight:bold;'.
							($v['user_color'] ? 'color:#'.$v['user_color'].';' : '').
							($user_banned ? 'text-decoration:line-through;' : '').';" href="javascript:void(0);"'.
							(!$user_banned && $v['user_id']!=$chat_user['user_id'] ? ' onClick="javascript:inserttext(\''.$v['username'].': \');"' : '').'>'.
							($user_rights[$v['user_id']][7] && $rights[8] ? '<i>'.$v['username'].'</i>' : $v['username']).'</a>'.
							($rights[15] && $v['user_id']!=$chat_user['user_id'] ? ' <a href="javascript:void(0);" onClick="javascript:aopts(\''.$v['user_id'].'\', \''.$chat_user['user_id'].'\', \''.
							($user_banned ? 'unban' : 'ban').'\');">&bull;</a> ' : '').'<b id="aopts'.$v['user_id'].'"></b>
						</div>';
					}
				}
			//}
		}
		else
		{
			$user_table= '<div><span class="textuser">'.$lang['CHAT_CANT_VIEWCHATU'].'</span></div>';
		}
		if($rights[4])//is_canviewchatm
		{
			/*if($config['ppkbb_chat_autokill_onrefresh'] && !$skip_lacheck && ($dt - $chat_user['lastaccess']) < $config['ppkbb_chat_murefresh'] - 1)
			{
				userkick(3);
			}
			else
			{*/
				$smiles=get_chat_smilies();
				intval($ppkbb_chat_messdisplay) ? '' : $ppkbb_chat_messdisplay=25;
				$sql='SELECT * FROM ' .PPKCHAT_MESSAGES_TABLE. " WHERE ".($rights[9] ? "(to_user='0' OR to_user='{$chat_user['user_id']}' OR (to_user!=0 AND user_id={$chat_user['user_id']}))" : "to_user='0'").(@$rights[14] && $get_gmode=='true' ? '' : " AND (room='{$forum_id}' OR room='0')").($ppkbb_chat_marchive[0] && $varch ? ' AND date > '.($dt - ($ppkbb_chat_marchive[0]*86400)) : '')." ORDER BY id DESC".($varch ? '' : " LIMIT {$ppkbb_chat_messdisplay}");
				$result=my_sql_query($sql);

				$last_mess_id=$i=0;
				while($row=mysql_fetch_array($result))
				{
					if($varch && $i<$ppkbb_chat_messdisplay)
					{
						$i+=1;
						continue;
					}
					if(@!$user_rights[$row['user_id']])
					{
						@$user_rights[$row['user_id']]=explode(' ', $v['rights']);
					}

					$row['message']=htmlspecialchars(stripslashes($row['message']));
					//$row['message']=wordwrap($row['message'], 32, '<wbr>', 1);
					$row['message'] = str_replace('---', '&minus;&minus;&minus;', $row['message']);

					if(@$user_rights[$row['user_id']][11] && $rights[11])
					{
						$row['message']=chat_smilies($row['message'], $smiles['match'], $smiles['replace'], $ppkbb_chat_maxsmiles);
					}
					$mess_addon='';

					if($rights[9] && $chat_user['user_id']==$row['to_user'])
					{
						$mess_addon=$lang['CHAT_PMIN'].(@$users[$row['user_id']] ? '<b style="color:#'.$users[$row['user_id']]['user_color'].'">'.$users[$row['user_id']]['username'].'</b>' : $row['username'].($varch ? " {$lang['CHAT_PMFOR']} " : ''));
					}
					else if($rights[9] && $row['to_user'] && $row['user_id']==$chat_user['user_id'])
					{
						$mess_addon=$lang['CHAT_PMOUT'].(@$users[$row['to_user']] ? '<b style="color:#'.$users[$row['to_user']]['user_color'].'">'.$users[$row['to_user']]['username'].'</b>' : '<a target="_blank" href="'.$script_path.'memberlist.'.$phpEx.'?mode=viewprofile&amp;u='.$row['to_user'].'">#'.$row['to_user'].'</a>').($varch ? " {$lang['CHAT_PMFROM']} " : '');
					}
					else
					{
						//$row['user_id'] ? '' : $row['user_color']='00FF00';
						//$row['user_id'] ? '' : $row['message']='<font style="color:#00FF00;">'.$row['message'].'</font>';
						$mess_addon=' <b style="'.($row['user_color'] ? 'color:#'.$row['user_color'] : '').';">'.$row['username'].'</b> ';
					}
					$varch ? $mess_addon.=($row['user_id'] > 1 ? ' [<a title="'.($rights[0] ? 'IP: '.long2ip($row['user_ip']) : '').'" target="_blank" href="'.$script_path.'memberlist.'.$phpEx.'?mode=viewprofile&amp;u='.$row['user_id'].'">#'.$row['user_id'].'</a>] ' : ' [<a title="'.($rights[0] ? 'IP: '.long2ip($row['user_ip']) : '').'" href="javascript:void(0);">#1</a>] ') : '';

					$message_table.= '
					<div style="width:100%;" id="p'.$row['id'].'">'.
						(!$varch && @$rights[14] && $get_gmode=='true' ? '<a href="javascript:void(0);" onclick="insertroom(\''.$row['room'].'\')">['.$row['room'].']</a>&nbsp;' : '').format_date($row['date']).$mess_addon.' '.
						(((@$rights[5] && $row['user_id']==$chat_user['user_id']) || (@$rights[13] && $row['user_id']) || (@$rights[13] && !$row['user_id'] && $ppkbb_chat_bot[2])) && !$varch  ? ' <a href="javascript:void(0);" onclick="delete_post(\''.$row['id'].'\')">['.
						($row['user_id']!=$chat_user['user_id'] ? 'x' : '<b>x</b>').']</a> ' : '').
						'<font class="c_mess">'.$row['message'].'</font>
					</div>';

					$last_mess_id2=$row['id'];
				}
				mysql_free_result($result);

				if($dt - $ppkbb_chat_last_cleanup > $ppkbb_chat_cleanup_interval)
				{
					if($ppkbb_chat_marchive[0])
					{
						$sql="SELECT id FROM ".PPKCHAT_MESSAGES_TABLE." WHERE date < ".($dt - ($ppkbb_chat_marchive[0]*86400)) ." ORDER BY id DESC LIMIT 1";
					}
					else
					{
						$sql="SELECT id FROM ".PPKCHAT_MESSAGES_TABLE." ORDER BY id DESC LIMIT {$ppkbb_chat_messdisplay}, 1";
					}
					$result=my_sql_query($sql);
					$last_mess_id=mysql_fetch_row($result);
					$last_mess_id = @$last_mess_id[0] ? $last_mess_id[0] : 0;

					if($last_mess_id)
					{
						$sql='DELETE FROM ' .PPKCHAT_MESSAGES_TABLE. " WHERE id <= '{$last_mess_id}'";
						$result=my_sql_query($sql);
					}

					$sql='UPDATE ' .CONFIG_TABLE. " SET config_value='{$dt}', is_dynamic='1' WHERE config_name='ppkbb_chat_last_cleanup'";
					$result=my_sql_query($sql);
				}
			//}
		}
		else
		{
			$message_table= '<div><span class="textmess">'.$lang['CHAT_CANT_VIEWCHATM'].'</span></div>';
		}
	}
	if(!$rights[6] && $dt - @$chat_user['lastpost'] > $ppkbb_chat_killtime)
	{
		userkick(4);
	}
}
else
{
	include($phpbb_root_path.'chat/include/cnochat.'.$phpEx);
}

if($varch)
{
	if($rights[16])
	{
		include($phpbb_root_path.'chat/include/cvarchchat.'.$phpEx);
		echo view_archive($message_table);
	}
}
else
{
	header('Content-type: text/html; charset=UTF-8');
	echo "$message_table---$last_mess_id2---$user_table---$dt---".($ppkbb_chat_murefresh*1000)."---$reason";
}

if($c)
{
	mysql_close($c);
}

$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;

exit();

//############################################################

function get_chat_smilies()
{
	global $smilies_path, $phpbb_root_path, $varch;
	static $match;
	static $replace;

	// See if the static arrays have already been filled on an earlier invocation
	if (!is_array($match))
	{
		$match = $replace = array();
		$smiles=array();

		$sql = 'SELECT *
			FROM ' . SMILIES_TABLE . '
			ORDER BY LENGTH(code) DESC';

		$result = my_sql_query($sql);

		while($row=mysql_fetch_array($result))
		{
			if (empty($row['code']))
			{
				continue;
			}

			// (assertion)
			$smiles['match'][] = '(?<=^|[\n .])' . preg_quote($row['code'], '#') . '(?![^<>]*>)';
			$smiles['replace'][] = '<img src="./'.$smilies_path.'/' . $row['smiley_url'] . '" alt="' . $row['code'] . '" title="' . $row['emotion'] . '" />';
		}
		mysql_free_result($result);
	}

	return $smiles;
}

function chat_smilies($text, $match, $replace, $max_smilies = 0)
{
	if (sizeof($match))
	{
		if ($max_smilies)
		{
			$num_matches = preg_match_all('#' . implode('|', $match) . '#', $text, $matches);
			unset($matches);

			if ($num_matches !== false && $num_matches > $max_smilies)
			{
				return $text;
			}
		}

		// Make sure the delimiter # is added in front and at the end of every element within $match
		$text = trim(preg_replace(explode(chr(0), '#' . implode('#' . chr(0) . '#', $match) . '#'), $replace, $text));
	}

	return $text;
}

function my_sql_query($query)
{
	global $startsqlcount, $c;

	$startsqlcount+=1;

	$result=@mysql_query($query, $c);

	if(!$result)
	{
		err('Unknown sql error');
		mysql_close($c);
	}

	return $result;
}

//From includes/utf/utf_tools.php
function utf8_strlen($text)
{
	// Since utf8_decode is replacing multibyte characters to ? strlen works fine
	return strlen(utf8_decode($text));
}

function err($str)
{
	global $c;

	header('Content-type: text/html; charset=UTF-8');
	echo $str;
	if($c)
	{
		mysql_close($c);
	}
	exit();
}

function userkick($n=0)
{
	global $last_mess_id2, $dt;

	$message_table='&nbsp';
	$user_table='&nbsp;';

	err("$message_table---$last_mess_id2---$user_table---$dt---3600000---$n");
}

function my_int_val($v=0)
{
	if(!$v || $v < 0)
	{
		return 0;
	}

	return @number_format($v+0, 0, '', '');
}

function my_split_config($config, $count=0, $type=false, $split='')
{
	$count=intval($count);
	$s_config=$count > 0 ? @explode($split ? $split : ' ', $config, $count) : @explode($split ? $split : ' ', $config);
	$count=$count > 0 ? $count : sizeof($s_config);
	if($count)
	{
		for($i=0;$i<$count;$i++)
		{
			if($type)
			{
				if(is_array($type) && @function_exists(@$type[$i]))
				{
					$s_config[$i]=call_user_func($type[$i], @$s_config[$i]);
				}
				else if(@function_exists($type))
				{
					$s_config[$i]=call_user_func($type, @$s_config[$i]);
				}
				else
				{
					$s_config[$i]=@$s_config[$i];
				}
			}
			else
			{
				$s_config[$i]=@$s_config[$i];
			}
		}
	}

	return $s_config;
}

//From /includes/sessions.php
function format_date($gmepoch, $format = false, $forcedate = false)
{
	static $midnight;
	static $date_cache;

	global $user_timezone, $user_dst, $user_dateformat, $lang;

	$date_format = $user_dateformat;
	$timezone = $user_timezone;
	$dst = $user_dst;

	$format = (!$format) ? $date_format : $format;
	$now = time();
	$delta = $now - $gmepoch;

	if (!isset($date_cache[$format]))
	{
		// Is the user requesting a friendly date format (i.e. 'Today 12:42')?
		$date_cache[$format] = array(
			'is_short'		=> strpos($format, '|'),
			'format_short'	=> substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1),
			'format_long'	=> str_replace('|', '', $format),
			'lang'			=> $lang['datetime'],
		);

		// Short representation of month in format? Some languages use different terms for the long and short format of May
		if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
		{
			$date_cache[$format]['lang']['May'] = $lang['datetime']['May_short'];
		}
	}

	// Zone offset
	$zone_offset = $timezone + $dst;

	// Show date <= 1 hour ago as 'xx min ago' but not greater than 60 seconds in the future
	// A small tolerence is given for times in the future but in the same minute are displayed as '< than a minute ago'
	/*if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now / 60) % 60) == (($gmepoch / 60) % 60)) && $date_cache[$format]['is_short'] !== false && !$forcedate && isset($lang['datetime']['AGO']))
	{
		return $lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
	}*/

	if (!$midnight)
	{
		list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $zone_offset));
		$midnight = gmmktime(0, 0, 0, $m, $d, $y) - $zone_offset;
	}

	if ($date_cache[$format]['is_short'] !== false && !$forcedate && !($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800))
	{
		$day = false;

		if ($gmepoch > $midnight + 86400)
		{
			$day = 'TOMORROW';
		}
		else if ($gmepoch > $midnight)
		{
			$day = 'TODAY';
		}
		else if ($gmepoch > $midnight - 86400)
		{
			$day = 'YESTERDAY';
		}

		if ($day !== false)
		{
			return str_replace('||', $lang['datetime'][$day], strtr(@gmdate($date_cache[$format]['format_short'], $gmepoch + $zone_offset), $date_cache[$format]['lang']));
		}
	}

	return strtr(@gmdate($date_cache[$format]['format_long'], $gmepoch + $zone_offset), $date_cache[$format]['lang']);
}
?>
